#!/usr/bin/env bash
# yitong.bai@qq.com

# Load Functions
. "${LIB_DIR}/lib.sh"
. "${LIB_DIR}/liblog.sh"
. "${LIB_DIR}/libinstall.sh"
. "${LIB_DIR}/libdownload.sh"
. "${LIB_DIR}/libtemplate.sh"

function KubernetesDownloadRole() {
  InstallCommandIfNotInstalled "python3" 
  InstallAnsibleIfNotInstalled "${CONF_DIR}/requirements/pip_requirements"
  
  InstallDockerIfNotInstalled
  RemoveDockerData
  StartupDockerService

  GetDefaultHostName
  GetDefaultIpAddress

  TemplateKubesprayDownloadInventory \
  "${CONF_DIR}/templates/inventory.ini.tpl" \
  "${DATA_DIR}/kubespray/inventory/sample/inventory.ini" \
  "${DOWNLOAD_HOSTNAME:-${DEFAULT_HOSTNAME}}" \
  "${DOWNLOAD_IPADDRESS:-${DEFAULT_IPADDRESS}}"

  # A bug of kuberspray, kuberspray does not mkdir /etc/kubernetes, fix it manully.
  mkdir -p "/etc/kubernetes"
  
  ChangeDownloadKubernetesVersion "${DATA_DIR}/kubespray" "${DOWNLOAD_KUBE_VERSION}"
  DownloadKubernetesInstallations "${DATA_DIR}/kubespray" "${DATA_DIR}/release"
  RenameKubernetesReleaseDir "${DATA_DIR}"

  DockerSaveImages "${DATA_DIR}/images" "${CONF_DIR}/requirements/images"
}

# Main 
function DownloadRole() {
  LoadFunctionsByOperatingSystemId
  LoadRolesByOperatingSystemId
  
  LoadGaeaConfiguration
  BootstrapOsRole
  RequirementsDownloadRole
  KubernetesDownloadRole
}
